window.selCheckedNum = 3;
const three = this.pageObj.getApiWidgetByAliasName('三维组件');
const organization = this.pageObj.getApiWidgetByAliasName('tree_organization');
const search_input = this.pageObj.getApiWidgetByAliasName('search_input');
const text = $(search_input.__box).find('.input-filter').val();
const prefix = this.pageObj.getVariables().find(v => v.name === 'environmentType')?.value || '';

const orgBtn = this.pageObj.getApiWidgetByAliasName('org_btn');
const groupBtn = this.pageObj.getApiWidgetByAliasName('group_btn');
const deviceBtn = this.pageObj.getApiWidgetByAliasName('device_btn');

const type = this.pageObj.getApiWidgetByAliasName('type_selection');
const select = type.widgetObj.getChecked()?.value;

$(this.__box).css("box-shadow", "inset 0 -5px 8px  var(--active-color)");
$(groupBtn.__box).css("box-shadow", "inset 0 0 5px var(--body-bg)");
$(orgBtn.__box).css("box-shadow", "inset 0 0 5px var(--body-bg)");


let fetchUrl = `/${prefix}Communication/pubCommunicationAccount/getAllAccountByEquipment`
const option = {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: "{}"
}

if (text) {
    option.body = JSON.stringify({ communicationName: text })
}
// /Communication/pubCommunicationAccount/listStructure
fetch(fetchUrl, option).then(res => res.json()).then(res => {
    const { result, resultCode } = res;
    if (resultCode === '000000') {
        const artemis = three.widgetObj.artemis;
        const dataSource = result.map(breakDataSource);
        if (select === 'all') {
            organization.widgetObj._option.dataSource = artemis.resetUsersStatus(dataSource);
        } else {
            organization.widgetObj._option.dataSource = artemis.filterUsersOnline(dataSource);
        }

        organization.widgetObj.render();
    }
});

function breakDataSource(item) {
    if(item.pointId || item.communicationName || item.personName) {
        item.id = item.personId;
        item.userId = item.account || item.communicationAccount;
        item.username = item.communicationName || item.personName;
        item.title = setTitle(item);
        item.treeId = item.userId;
        item.disabled = true
    } else if(item.orgId || item.orgName) {
        item.title = `${item.orgName}[${item.count}]`;
        item.id = item.orgId;
        item.treeId = item.orgId;
    } else if (item.deptId || item.deptName) {
        item.title = `${item.deptName}[${item.count}]`;
        item.id = item.deptId;
        item.treeId = item.deptId;
    } else if (item.distributionGroupId || item.distributionGroupName) {
        item.title = `${item.distributionGroupName}[${item.count}]`;
        item.id = item.distributionGroupId;
        item.treeId = item.distributionGroupId;
    } else if (item.equipmentTypeName || item.equipmentTypeCode) {
        item.title = `${item.equipmentTypeName}[${item.count}]`;
        item.id = item.equipmentTypeCode;
        item.treeId = item.equipmentTypeCode;
    }

    if (item.orgList) item.children = item.orgList.map(breakDataSource)
    if (item.groupList) item.children = item.groupList.map(breakDataSource)
    if (item.personList) item.children = item.personList.map(breakDataSource)
    if (item.accountData) item.children = item.accountData.map(breakDataSource)
    if (item.equipmentType) item.children = item.equipmentType.map(breakDataSource)

    return item
}

function setTitle ({ typeCode, communicationName, personName, username, userId } = {}) {
    let title = '';

    if (typeCode === 'dispatchDesk') title += '调度台';

    title += (communicationName || username || personName);

    if (userId) title += `(${userId})`;

    return title
}